// services/client-generator.js
const deepseek = require('./LLM/deepseek');
const prompts = require('./LLM/prompts');

class Code2ClientCode {
    /**
     * 生成客户端适配代码
     * @param {string} backendCode 后端代码
     * @param {string} techStack 技术栈(axios/fetch/requests等)
     * @returns {Promise<string>} 生成的客户端代码
     */
    async generateClientCode(backendCode, techStack = 'axios') {
        techStack = techStack.toLowerCase();
        try {
            const response = await deepseek.call(
                prompts.Code2ClientCode(backendCode, techStack),
                { temperature: 0.3 }
            );

            return this._extractCode(response);
        } catch (error) {
            console.error('生成客户端代码失败:', error);
            throw new Error('生成客户端代码失败: ' + error.message);
        }
    }

    _extractCode(content) {
        // 通用代码块匹配，不限定语言
        const match = content.match(/```[\s\S]*?\n([\s\S]*?)```/);
        return match ? match[1].trim() : content;
    }
}

module.exports = new Code2ClientCode();